package com.icss.head.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.common.servlet.BaseServlet;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.service.IGoodsDbService;
import com.icss.service.impl.GoodsDbServiceImpl;
import com.icss.vo.GymMessage;
import com.icss.vo.GymMessageEnum;
import com.icss.vo.GymUsers;

/**
 * 商品前台操作
 * 
 * @author 郝厅
 */
@WebServlet(urlPatterns = "/head/GoodsHeadServlet")
public class GoodsHeadServlet extends BaseServlet
{
	private IGoodsDbService goodsDbService;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.goodsDbService = new GoodsDbServiceImpl();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("messageList".equalsIgnoreCase(method))
		{
			/* messageList */
			messageList(request, response);
		} else if ("messageInsert".equalsIgnoreCase(method))
		{
			/* messageInsert */
			messageInsert(request, response);
		}else if ("messageInsertSubmit".equalsIgnoreCase(method))
		{
			/* messageInsertSubmit */
			messageInsertSubmit(request, response);
		} else if ("messageUpdate".equalsIgnoreCase(method))
		{
			/* messageUpdate */
			messageUpdate(request, response);
		} 
	}
	
	/**
	 * 评论列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String messageList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		GymUsers users = (GymUsers) this.findObjfromSession(request, "users");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("usersId", users.getId());
		condMap.put("status", GymMessageEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<GymMessage> apiResponse = this.goodsDbService.findCondListMessageService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goods/messageList";
	}

	/**
	 * 打开评论页面
	 * @param request
	 * @param response
	 */
	public String messageInsert(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		/* 页面跳转 */
		return "/head/goods/messageInsert" ; 
	}
	
	/**
	 * 评论提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String messageInsertSubmit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		GymUsers users = (GymUsers) this.findObjfromSession(request, "users");
		
		GymMessage message = new GymMessage();
		/* 创建对象 */
		
		ApiResponse<Object> apiResponseDb = this.goodsDbService.saveOneMessageService(message);
		if(apiResponseDb.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			return "redirect:/head/goods/messageList.htm" ; 
		}
		return this.messageInsert(request,response);
	}

	/**
	 * 评论更新
	 * @param request
	 * @param response
	 */
	public String messageUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<GymMessage> apiResponse = this.goodsDbService.findOneMessageService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goods/messageInfo" ; 
	}
}
